var IDCard = /** @class */ (function () {
    // 构造方法
    function IDCard(IDCard) {
        //身份id
        this.areaID = {
            11: "北京",
            12: "天津",
            13: "河北",
            14: "山西",
            15: "内蒙古",
            21: "辽宁",
            22: "吉林",
            23: "黑龙江",
            31: "上海",
            32: "江苏",
            33: "浙江",
            34: "安徽",
            35: "福建",
            36: "江西",
            37: "山东",
            41: "河南",
            42: "湖北",
            43: "湖南",
            44: "广东",
            45: "广西",
            46: "海南",
            50: "重庆",
            51: "四川",
            52: "贵州",
            53: "云南",
            54: "西藏",
            61: "陕西",
            62: "甘肃",
            63: "青海",
            64: "宁夏",
            65: "新疆",
            71: "台湾",
            81: "香港",
            82: "澳门",
            91: "国外"
        };
        //性别
        this.sexMap = {
            0: "女",
            1: "男"
        };
        this.info = {};
        this.status = {};
        console.log('hi');
        this.IDCard = IDCard;
    }
    /**
     * 身份证验证
     */
    IDCard.prototype.check = function () {
        var _this_1 = this;
        var _this = this;
        var str = [
            'getLenth',
            'validate',
            'getArea',
            'getBirthday',
            'getSex',
            'getAge'
        ];
        //循环遍历 验证
        str.forEach(function (v) {
            var rs = _this_1[v]();
            if (rs.status == false) {
                _this.status = rs;
                return;
            }
        });
        //判断是否有错误信息
        if (this.isEmpty(this.status)) {
            this.status = {
                status: true,
                message: 'ok',
                data: this.info
            };
        }
        return this.status;
    };
    /**
     * 获取出生地区
     */
    IDCard.prototype.getArea = function () {
        //过滤数据 x 替换成 a
        var IDCard = this.IDCard.replace(/x$/i, "a");
        // 地区列表 下标
        var index = IDCard.substr(0, 2);
        // 判断地区是否合法
        if (this.areaID[index] == undefined) {
            console.log('你的身份证地区非法');
            return {
                status: false,
                message: '你的身份证地区非法!'
            };
        }
        this.info.area = this.areaID[index];
        return {
            status: true,
            message: this.areaID[index]
        };
    };
    /**
     * 获取出生年月日
     */
    IDCard.prototype.getBirthday = function () {
        //提取出生日期
        var sBirthday = this.IDCard.substr(6, 4) + "-" + Number(this.IDCard.substr(10, 2)) + "-" + Number(this.IDCard.substr(12, 2));
        var d = new Date(sBirthday.replace(/-/g, "/"));
        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
            console.log('身份证上的出生日期非法');
            return {
                status: false,
                message: '身份证上的出生日期非法!'
            };
        }
        this.info.birthday = this.IDCard.substr(6, 4) + "-" + this.IDCard.substr(10, 2) + "-" + this.IDCard.substr(12, 2);
        return {
            status: true,
            message: this.IDCard.substr(6, 4) + "-" + this.IDCard.substr(10, 2) + "-" + this.IDCard.substr(12, 2)
        };
    };
    /**
     * 获取性别
     */
    IDCard.prototype.getSex = function () {
        //检测性别
        var sex;
        if (this.IDCard.length == 15) {
            sex = this.sexMap[this.IDCard.substring(14, 15) % 2];
        }
        else if (this.IDCard.length == 18) {
            sex = this.sexMap[this.IDCard.substring(14, 17) % 2];
        }
        else {
            //不是15或者18,null
            return {
                status: false,
                message: '性别格式错误'
            };
        }
        this.info.sex = sex;
        return {
            status: true,
            message: sex
        };
    };
    /**
     * 检测身份证长度
     */
    IDCard.prototype.getLenth = function () {
        //检测身份证长度
        if (!/^\d{17}(\d|x)$/i.test(this.IDCard)) {
            console.log('你输入的身份证长度或格式错误');
            return {
                status: false,
                message: '你输入的身份证长度或格式错误!'
            };
        }
        return {
            status: true,
            message: ''
        };
    };
    /**
     * 检测身份证格式
     */
    IDCard.prototype.validate = function () {
        // 检测身份证 格式
        var iSum = 0;
        for (var i = 17; i >= 0; i--) {
            iSum += (Math.pow(2, i) % 11) * parseInt(this.IDCard.charAt(17 - i), 11);
        }
        if (iSum % 11 != 1) {
            console.log('你输入的身份证号非法');
            return {
                status: false,
                message: '你输入的身份证号非法!'
            };
        }
        return {
            status: true,
            message: '校验成功！'
        };
    };
    /**
     * 获取年龄
     */
    IDCard.prototype.getAge = function () {
        var birthStr = this.IDCard.substr(6, 4) + "-" + this.IDCard.substr(10, 2) + "-" + this.IDCard.substr(12, 2);
        var r = birthStr.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if (r == null)
            return '';
        var d = new Date(r[1], r[3] - 1, r[4]);
        var age = 0;
        if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
            var Y = new Date().getFullYear();
            age = (Y - r[1]);
        }
        this.info.age = age;
        return {
            status: true,
            message: age
        };
    };
    /*
    * 判断是否为空
    * obj 要判断的 变量 或者 对象 字符串
    * */
    IDCard.prototype.isEmpty = function (obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        }
        else {
            return false;
        }
    };
    return IDCard;
}());
/**
 * 演示数据
 */
var idCard = new IDCard('371325199509093045');
var rs = idCard.check();
console.log(rs);
